package com.bob.imdservice.socket;

import com.bob.imdservice.manager.ThreadManager;
import com.bob.imdservice.model.FileLog;
import com.bob.imdservice.thread.DownThread;
import com.bob.imdservice.thread.ServerThread;
import com.bob.imdservice.thread.UpThread;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;


/**
  * 连接服务
  * @author charlie
  * create at 2017/7/10 1:56
  */

public class ConnectServer {
	/**
	 * 服务端口
	 */
	private static ServerSocket serverSocket;
	/**
	 * 下载端口
	 */
	private static ServerSocket downSocket;
	/**
	 * 上传端口
	 */
	private static ServerSocket upSocket;
	/**
	 * 服务端口
	 */
	private static final int port = 8624;
	/**
	 * 下载端口
	 */
	private static final int down_port = 8625;
	/**
	 * 上传端口
	 */
	private static final int up_port = 8626;


	// 存放断点数据，最好改为数据库存放
	private Map<Long, FileLog> datas = new HashMap<Long, FileLog>();
	/**
	 * 连接服务器的线程
	 */
	private Runnable connectRunnable = new Runnable() {

		@Override
		public void run() {
			while (true) {
				System.out.println("查询服务器正在等待连接...");
				Socket socket;
				try {
					socket = serverSocket.accept();// 一直等待连接，没有连接，线程会处于阻塞状态
					// 对应每一个请求，开启另外的线程去处理
					ThreadManager.getInstance().getExecutorService()
							.execute(new ServerThread(socket));
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	};
	// 开启一个下载线程等待连接，即从电脑下载东西到手机
	private Runnable downloadRunnable = new Runnable() {

		@Override
		public void run() {
			while (true) {
				System.out.println("下载服务器正在等待连接...");
				Socket socket;
				try {
					socket = downSocket.accept();
					// 对应每一个请求，开启另外的线程去处理
					ThreadManager.getInstance().getExecutorService()
							.execute(new DownThread(socket));
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	};
	// 开启一个上传线程等待连接，从手机端上传数据到电脑
	private Runnable uploadRunnable = new Runnable() {

		@Override
		public void run() {
			while (true) {
				System.out.println("上传服务器正在等待连接...");
				Socket socket;
				try {
					socket = upSocket.accept();
					// 对应每一个请求，开启另外的线程去处理
					ThreadManager.getInstance().getExecutorService()
							.execute(new UpThread(socket, datas));
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	};

	public ConnectServer() throws IOException {
		serverSocket = new ServerSocket(port);
		downSocket = new ServerSocket(down_port);
		upSocket = new ServerSocket(up_port);
		datas = new HashMap<Long, FileLog>();
		System.out.println("服务器连接启动.");
	}


	/**
	 * 开始服务
	 * 
	 * @throws IOException
	 */
	public void service() throws IOException {

		new Thread(connectRunnable).start();
		new Thread(downloadRunnable).start();
		new Thread(uploadRunnable).start();
	}

}
